<?php

namespace App\Service;

class SignService
{
    const API_KEY = 'bot_xyhs_0511';

    public static $instance = null;

    public static function getInstance()
    {
        if (self::$instance == null) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * 获取签名
     * @param $param
     * @return string
     */
    public function getSign($param)
    {
        ksort($param);
        $paramStr = implode('', $param);
        return md5($paramStr . self::API_KEY);
    }

    /**
     * 格式化请求参数
     * @param $param
     * @return string
     */
    public function initParamGet($param)
    {
        $param['time'] = time();
        $param['sign'] = $this->getSign($param);

        $paramArr = [];
        foreach ($param as $key => $value) {
            $paramArr[] = $key . "=" . urlencode($value);
        }
        return join("&", $paramArr);
    }

    /**
     * 格式化请求参数post
     * @param $param
     * @return array
     */
    public function initParamPost($param)
    {
        $param['time'] = time();
        $param['sign'] = $this->getSign($param);
        return $param;
    }
}